import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import api from '../utils/api'

export const useUserStore = defineStore('user', () => {
  const token = ref(localStorage.getItem('token') || '')
  const user = ref(JSON.parse(localStorage.getItem('user') || 'null'))
  const isAdmin = ref(localStorage.getItem('isAdmin') === 'true')

  const isLoggedIn = computed(() => !!token.value)
  const userInfo = computed(() => user.value)

  // 登录
  const login = async (loginData) => {
    try {
      const response = await api.post('/auth/user/login', loginData)
      if (response.data.code === 200) {
        token.value = response.data.data.token
        user.value = response.data.data.user
        isAdmin.value = false
        localStorage.setItem('token', token.value)
        localStorage.setItem('user', JSON.stringify(user.value))
        localStorage.setItem('isAdmin', 'false')
        return { success: true, message: '登录成功' }
      } else {
        return { success: false, message: response.data.message }
      }
    } catch (error) {
      return { success: false, message: '登录失败，请检查网络连接' }
    }
  }

  // 管理员登录
  const adminLogin = async (loginData) => {
    try {
      const response = await api.post('/auth/admin/login', loginData)
      if (response.data.code === 200) {
        token.value = response.data.data.token
        user.value = response.data.data.admin
        isAdmin.value = true
        localStorage.setItem('token', token.value)
        localStorage.setItem('user', JSON.stringify(user.value))
        localStorage.setItem('isAdmin', 'true')
        return { success: true, message: '登录成功' }
      } else {
        return { success: false, message: response.data.message }
      }
    } catch (error) {
      return { success: false, message: '登录失败，请检查网络连接' }
    }
  }

  // 注册
  const register = async (registerData) => {
    try {
      const response = await api.post('/auth/user/register', registerData)
      if (response.data.code === 200) {
        return { success: true, message: '注册成功' }
      } else {
        return { success: false, message: response.data.message }
      }
    } catch (error) {
      return { success: false, message: '注册失败，请检查网络连接' }
    }
  }

  // 获取用户信息
  const getUserInfo = async () => {
    try {
      const response = await api.get('/user/info')
      if (response.data.code === 200) {
        user.value = response.data.data
        localStorage.setItem('user', JSON.stringify(user.value))
        return { success: true, data: user.value }
      } else {
        console.error('获取用户信息失败:', response.data.message)
        return { success: false, message: response.data.message }
      }
    } catch (error) {
      console.error('获取用户信息异常:', error)
      return { success: false, message: '获取用户信息失败' }
    }
  }

  // 退出登录
  const logout = () => {
    token.value = ''
    user.value = null
    isAdmin.value = false
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    localStorage.removeItem('isAdmin')
  }

  return {
    token,
    user,
    isAdmin,
    isLoggedIn,
    userInfo,
    login,
    adminLogin,
    register,
    getUserInfo,
    logout
  }
})
